<?php

namespace app\app\controller;

use app\app\model\BiddingProvinceModel;
use app\BaseController;
use think\db\exception\DbException;

class BiddingProvince extends BaseController
{
    private $model;

    /**
     * initialize
     * @return void
     */
    public function initialize()
    {
        $this->model = BiddingProvinceModel::class;
    }

    /**
     * C&U
     * @return void
     */
    public function save()
    {
        $id = input('id', '');
        $param = $this->request->param();

        if (empty($id)) {
            $model = new $this->model();
        } else {
            $model = $this->model::where('id', $id);
        }

        $model->save($param);

        $this->success('success');
    }

    /**
     * R
     * @return void
     * @throws DbException
     */
    public function list()
    {
        $where = [];

        $id = input('bidding_id', '');
        empty($id) && $this->error('请传入id');
        $where[] = ['bidding_id', '=', $id];

        $project_name = input('project_name', '');
        if (!empty($project_name)) $where[] = ['project_name', 'like', '%' . $project_name . '%'];

        $name = input('name', '');
        if (!empty($name)) $where[] = ['name', 'like', '%' . $name . '%'];

        $bidding_time = input('bidding_time', '');
        if (!empty($bidding_time)) {
            $where[] = ['declare_end_time', '>=', $bidding_time];
            $where[] = ['declare_start_time', '<=', $bidding_time];
        }

//        $page_size = input('pageSize', '20');

        $res = $this->model::where($where)
//            ->paginate($page_size);
            ->select();

        $tmp = [];
        foreach ($res->toArray() as $v) {
            $tmp[$v['pc_name']][] = $v;
        }
        $data = [];
        foreach ($tmp as $k => $v) {
            $data[] = [
                'pc_name' => $k,
                'children' => $v,
            ];
        }

//        empty($data) && $this->error('查询为空');

        $this->success('success', ['data' => $data]);
    }

    /**
     * detail
     * @param $id
     * @return void
     */
    public function detail($id = 0)
    {
        !$id && $this->error('请传入id');

        $data = $this->model::where('id', $id)->find();
        empty($data) && $this->error('查询为空');

        $this->success('success', $data);
    }

    /**
     * D
     * @return void
     * @throws DbException
     */
    public function del($id = 0)
    {
        !$id && $this->error('请传入id');

        $this->model::where('id', $id)->delete();

        $this->success('success');
    }
}